package com.preguicoso.client.backend.pedidos.confirmarMesa;

import java.util.List;

import com.gargoylesoftware.htmlunit.util.Cookie;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Widget;
import com.preguicoso.client.checkout.CheckoutService;
import com.preguicoso.client.checkout.CheckoutServiceAsync;
import com.preguicoso.shared.entities.MesaBean;
import com.preguicoso.shared.entities.PedidoBean;

public class ConfirmarMesaItem extends Composite  {

	private static ConfirmarMesaItemUiBinder uiBinder = GWT
			.create(ConfirmarMesaItemUiBinder.class);

	interface ConfirmarMesaItemUiBinder extends
			UiBinder<Widget, ConfirmarMesaItem> {
	}
	@UiField InlineLabel noMesa;
	@UiField Button sim;
	@UiField Button nao;
	
	private final CheckoutServiceAsync checkoutService = GWT.create(CheckoutService.class);

	MesaBean mesa;
	
	public ConfirmarMesaItem() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public ConfirmarMesaItem(MesaBean mesaBean) {
		initWidget(uiBinder.createAndBindUi(this));
		noMesa.setText(mesaBean.getMesa()+"");
		mesa = mesaBean;
	}
	
	@UiHandler("sim")
	void onSimClick(ClickEvent event) {
		checkoutService.confirmarMesa(mesa.getId()+"",
		
		new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
			@Override
			public void onSuccess(Boolean result) {
				Window.alert("Mesa "+mesa.getMesa()+" confirmada com sucesso");
			}
		});
	}	
	@UiHandler("nao")
	void onNaoClick(ClickEvent event) {
		checkoutService.naoConfirmarMesa(mesa.getId()+"", new AsyncCallback<Boolean>() {
			
			@Override
			public void onSuccess(Boolean result) {
				if(result)
					Window.alert("Mesa "+mesa.getMesa()+"não confirmada");
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}


}
